package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsPackingMapper;
import com.jy.wms.pojo.SysDictionary;
import com.jy.wms.pojo.WmsPacking;
import com.jy.wms.pojo.WmsPackingDetailed;
import com.jy.wms.pojo.WmsStacking;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
    * @ClassName: WmsPackingService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author JaneLiu
    * @Date Wed Sep 06 10:34:42 CST 2017
    */

@Service
public class WmsPackingService extends BaseService<WmsPackingMapper,WmsPacking> {

     /**
     * 新增包装
     * @param wmsPacking
     * @return int
     */
     @BaseServiceDescribe(moduleName = "包装管理",btnName = "新增包装")
     @Transactional
    public void baseInsert( WmsPacking wmsPacking) throws Exception{
         List<WmsPacking> wmsPackingList = new ArrayList<>();
        Integer flaga2=0;
         Integer flaga1 = 0;
         List<WmsPacking> arraymx = wmsPacking.getList();//取明细集合
         for (WmsPacking packing : arraymx) {
             packing.setMxid(UUIDUtils.uuid());
             packing.setCkid(wmsPacking.getCkid());
             packing.setSsbzid(wmsPacking.getId());
             flaga1 = dao.baseInsert_mx(packing);//保存明细
             SysDictionary sysDictionary = selectType(packing, "wms_packing_detailed_bzlx");//查字典显示show_的字段
             packing.setShow_bzlx(sysDictionary.getD_name());
             if (flaga1 > 0) {
                 flaga2++;
                 wmsPackingList.add(packing);
             } else {
                 rollBack(wmsPackingList);
                 throw new ServiceException("新增包装明细信息失败");
            }
         }
         if (flaga2 == wmsPacking.getList().size()) {
             Integer flaga = dao.baseInsert(wmsPacking);//保存包装
             if (flaga > 0) {
                 throw new SucessException("新增包装信息成功");
             } else {
                 rollBack(wmsPackingList);
                 throw new ServiceException("新增包装信息失败");
             }
        }else{
             rollBack(wmsPackingList);
             throw new ServiceException("新增包装明细信息失败");
        }
    }

    /**
     * 包装添加回滚
     *
     * @param wmsPackingList
     */
    public void rollBack(List<WmsPacking> wmsPackingList) throws Exception {
        for (WmsPacking wmsPacking : wmsPackingList) {
            Integer j = dao.deleteMx(wmsPacking.getMxid());
            if (j <= 0) {
                throw new ServiceException("新增包装明细回滚失败");
            }
        }
    }
    /**
    * 分页查询
    * @param entity 实体
    * @return Page<WmsPacking>
    */
    @BaseServiceDescribe(moduleName = "包装管理",btnName = "分页查询包装")
    public Page<WmsPacking> baseSelectPage(WmsPacking entity) throws ServiceException {
        try{
            PageHelper.startPage(entity.getPage(),entity.getRows());
            return (Page<WmsPacking>)dao.baseSelectPage(entity);
        }catch(Exception e){
            throw  new ServiceException("查询包装信息失败",e);
        }
    }
   /**
    * 修改查询（根据主键ID查询）
    * @param id 主键ID
    * @return WmsPacking
    */
    public WmsPacking baseSelectByPrimaryKey(String id){
        List<WmsPacking> list=dao.baseSelectByPrimaryKey_mx(id);//查询明细id
        WmsPacking wmsPacking=dao.baseSelectByPrimaryKey(id);//查询包装id
        wmsPacking.setList(list);
        return wmsPacking;
    }
   /**
    *修改包装及明细
    * @param wmsPacking 主键ID列表
    * @return int
    */
   @BaseServiceDescribe(moduleName = "包装管理",btnName = "编辑包装")
   public void baseUpdateByPrimaryKeySelectivexg(WmsPacking wmsPacking) throws Exception{
       Integer flaga2=0;
       Integer flaga= dao.baseUpdateByPrimaryKeySelective(wmsPacking);//保存包装
        if(flaga>0){
            List<WmsPacking> arraymx =  wmsPacking.getList();//取明细集合
            for (WmsPacking packing:arraymx){
                flaga2=dao.baseUpdateByPrimaryKeySelective_mx(packing);//保存明细
                SysDictionary sysDictionary=selectType(packing,"wms_packing_detailed_bzlx");//查字典显示show_的字段
                packing.setShow_bzlx(sysDictionary.getD_name());
                if(flaga2<0){
                    throw new ServiceException("编辑包装明细信息失败");
                }
            }
            throw new SucessException("编辑包装信息成功");
        }else {
            throw new ServiceException("编辑包装信息失败");
        }
   }
        /**
         * 根据货主Id查询
         * @param sshzid 实体
         * @return List<WmsPacking>
         */
        public List<WmsPacking>  baseSelectBySshzid(String mc,String sshzid){
            return dao.baseSelectBySshzid(mc,sshzid);
        }

    /**
     *修改（根据主键ID修改绑定货品ID）

     * @return int
     */
   public Integer baseUpdateByPrimaryKeySelective( WmsPacking entity) {
        return dao.baseUpdateByPrimaryKeySelective(entity);
    }
    //添加去重
    public WmsPacking searchAdd(String bzmc,String ckid,String sshzid){
        return dao.searchAdd(bzmc,ckid,sshzid);
    }
    //修改去重
    public WmsPacking searchAdds(String bzmc,String id,String ckid,String sshzid){
        return dao.searchAdds(bzmc,id,ckid,sshzid);
    }
    //查字典
    public SysDictionary selectType(WmsPacking wmsPacking,String type){
        return dao.selectType(wmsPacking,type);
    }
    //查货主名
    public WmsPacking addSearch(String id)  {
        return dao.addSearch(id);
    }
    /**
     *  堆码中添加去重库位高度
     * @param kwgd
     * @param ckid
     * @param hzid
     * @param bzid
     * @return
     */

    public WmsStacking searchStackAdd(Double kwgd, String ckid, String hzid, String bzid) {
        return dao.searchStackAdd(kwgd, ckid, hzid, bzid);
    }
    /**
     * 堆码中编辑去重库位高度
     * @param id
     * @param kwgd
     * @param ckid
     * @param hzid
     * @return
     */
    public WmsStacking searchStackAdds(String id, String bzid, Double kwgd, String ckid, String hzid) {
        return dao.searchStackAdds(id, bzid, kwgd, ckid, hzid);
    }


    @BaseServiceDescribe(moduleName = "包装管理",btnName = "修改堆码")
    @Transactional
    public void editStacking( WmsStacking wmsStacking) throws Exception{
        Integer flaga=dao.editStacking(wmsStacking);
        if (flaga > 0) {
            throw new SucessException("修改堆码信息成功");
        } else {
            throw new ServiceException("修改堆码信息失败");
        }
    }

    /**
     * 查看单个堆码信息(编辑前的查询用)
     * @param id
     * @return
     */
    public WmsStacking getWmsStacking(String id){
        return dao.getWmsStacking(id);
    }

    @BaseServiceDescribe(moduleName = "包装管理",btnName = "新增堆码")
    @Transactional
    public void insertStacking( WmsStacking wmsStacking) throws Exception{
        wmsStacking.setId(UUIDUtils.uuid());
        Integer flaga=dao.insertStacking(wmsStacking);
        if (flaga > 0) {
            throw new SucessException("新增堆码信息成功");
        } else {
            throw new ServiceException("新增堆码信息失败");
        }
    }
    //删除
    @BaseServiceDescribe(moduleName = "堆码管理",btnName = "删除堆码")
    public void deleteStacking( List<String> idList ) throws Exception{
        Integer i = dao.deleteStacking( idList );
        if(i>0){
            throw new SucessException("删除堆码成功");
        }else{
            throw new ServiceException("删除堆码失败");
        }
    }
    /**
     * 分页查询堆码列表
     * @param entity
     * @return
     */
    @BaseServiceDescribe(moduleName = "堆码管理",btnName = "分页查询堆码")
    public Page<WmsStacking> getListStacking(WmsStacking entity) throws ServiceException {
        try{
            PageHelper.startPage(entity.getPage(),entity.getRows());
            return (Page<WmsStacking>)dao.getListStacking(entity);
        }catch(Exception e){
            throw  new ServiceException("查询堆码信息失败",e);
        }
    }
}
